/** 
 * enable uart tx
 * hzheng, 2023.08.02
 */

void mydelay(void);

#define GPIO_ODR *((volatile unsigned int *)0x50006000)

#define UART0_DR *((volatile unsigned int *)0x40008000)
#define UART0_IBRD *((volatile unsigned int *)0x40008024)
#define UART0_FBRD *((volatile unsigned int *)0x40008028)
#define UART0_LCR_H *((volatile unsigned int *)0x4000802c)
#define UART0_CR *((volatile unsigned int *)0x40008030)

unsigned int gValue=0x30;

void main() {
  
  unsigned int i;
  GPIO_ODR = 0x00;   
  //
  //init uart0
  //baudrate=115200, 115200=fclk/(16*n)
  //fclk = 80M or 160M
  //n1=80M/16/115200=43.40278; 0.40278*64+0.5=26
  //n2=160M/16/115200=86.80556; 0.80556*64+0.5=52
  UART0_CR = 0;
  UART0_LCR_H = 0x0060; //bit[6:5]=11
  UART0_IBRD = 43;
  UART0_FBRD = 26;
  UART0_CR = 0x0301; //txen, rxen, uarten
  
  UART0_DR = 0x41;
  
  while(1) {
    UART0_DR = gValue;    
    gValue ++;
    
    GPIO_ODR ^= 0x04;
    mydelay();
  }
}

void mydelay(void) {
  unsigned int i;
  for (i=0;i<10000000;i++) {
    __asm__ __volatile__("nop");
  }
}

